package com.cyk.comicforumjava.demos.service.impl;

import cn.hutool.log.Log;
import com.cyk.comicforumjava.demos.base.ResponseResult;
import com.cyk.comicforumjava.demos.dto.UserDto;
import com.cyk.comicforumjava.demos.enits.*;
import com.cyk.comicforumjava.demos.mapper.LoginMapper;
import com.cyk.comicforumjava.demos.mapper.MessageMapper;
import com.cyk.comicforumjava.demos.mapper.PostMapper;
import com.cyk.comicforumjava.demos.properties.JwtTokenProperties;
import com.cyk.comicforumjava.demos.service.PostService;
import com.cyk.comicforumjava.demos.utils.IdProcess;
import com.cyk.comicforumjava.demos.utils.JwtUtils;
import com.cyk.comicforumjava.demos.utils.ThreadLocalUtil;
import com.cyk.comicforumjava.demos.vo.PostVo;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
@Transactional
@Slf4j
@Service
public class PostServiceImpl implements PostService {
    @Autowired
    private PostMapper postMapper;
    @Autowired
    private JwtTokenProperties jwtTokenProperties;
    @Autowired
    private LoginMapper loginMapper;
    @Autowired
    MessageMapper messageMapper;

    @Override
    public ResponseResult getPost(String token,String postId) {
        log.info("token:{}",token);
        if("token".equals(token)){
            PostVo postVo=postMapper.getPost(postId);
            postVo.setPostAvater(loginMapper.selectByUserId(postVo.getPostMaster()).getAvator());
            postVo.setBeSupport(false);
            postVo.getCommentList().forEach(commentVo -> commentVo.setBeSupport(false));
            return ResponseResult.success(postVo);
        }else{
             //Map<String,Object> map = JwtUtils.parseJWT(jwtTokenProperties.getSecretKey(), token);
             //Integer userId= (Integer) map.get("id");
             Support supporter=new Support();
             supporter.setUserId(ThreadLocalUtil.get());
             supporter.setPostId(postId);
             Support support=postMapper.getSupport(supporter);
             PostVo postVo=postMapper.getPost(postId);
             log.info("postVo:{}",postVo.getCommentList());
            postVo.setPostAvater(loginMapper.selectByUserId(postVo.getPostMaster()).getAvator());
             if(support==null){
                 postVo.setBeSupport(false);
             }else{
                 postVo.setBeSupport(true);
             }
             if(postVo.getCommentList()!=null){
                 postVo.getCommentList().forEach(commentVo -> {
                     if(commentVo.getParentId()!=null&& commentVo.getParentId().charAt(0) == 'C'){
                         commentVo.setParentName(postMapper.selectComment(commentVo.getParentId()).getUserName());
                     }
                     supporter.setPostId(commentVo.getCommentId());
                     Support commentSupport=postMapper.getSupport(supporter);
                     if(commentSupport==null){
                         commentVo.setBeSupport(false);
                     }else{
                         commentVo.setBeSupport(true);
                     }
                 });
             }

             log.info("postVo的属性:{}",postVo);
             return ResponseResult.success(postVo);
        }

    }


    @Transactional
    @Override
    public ResponseResult support(Support support) {
        try{


            if(support.getPostId().substring(0,1).equals("C")){
                Comment comment=postMapper.selectComment(support.getPostId());
                log.info("comment:{}",comment);
                comment.setCommentSupport(comment.getCommentSupport()+1);
                postMapper.updateComment(comment);
                Message message=new Message(support);
                User user=loginMapper.selectByUserId(support.getUserId());
                message.setUserlId(postMapper.selectComment(support.getPostId()).getUserId());
                message.setSenderId(user.getId());
                message.setContent("<p>"+user.getUsername()+"赞了你的评论</p>");
                messageMapper.addMessage(message);
                support.setMessageId(message.getId());
                postMapper.newSupport(support);
            }else{
                Post post=postMapper.getPost(support.getPostId());
                post.setSupport(post.getSupport()+1);
                postMapper.updatePost(post);
                Message message=new Message(support);
                User user=loginMapper.selectByUserId(support.getUserId());
                message.setUserlId(postMapper.getPost(support.getPostId()).getPostMaster());
                message.setSenderId(user.getId());
                message.setContent("<p>"+user.getUsername()+"赞了你的帖子</p>");
                messageMapper.addMessage(message);
                support.setMessageId(message.getId());
                postMapper.newSupport(support);
            }
            User user=loginMapper.selectByUserId( support.getUserId());
            user.setSupport(user.getSupport()+1);
            loginMapper.updateUser(user);
        }catch (Exception e){
            throw e;
        }

        return ResponseResult.success();
    }

    @Override
    public ResponseResult delSupport(Support support) {
        try{
            support=postMapper.getSupport(support);
            postMapper.delSupport(support);
            if(support.getPostId().substring(0,1).equals("C")){
                Comment comment=postMapper.selectComment(support.getPostId());
                comment.setCommentSupport(comment.getCommentSupport()-1);
                postMapper.updateComment(comment);
                messageMapper.deleteMessage(support.getMessageId());
            }else{
                Post post=postMapper.getPost(support.getPostId());
                post.setSupport(post.getSupport()-1);
                postMapper.updatePost(post);
                messageMapper.deleteMessage(support.getMessageId());
            }

            User user=loginMapper.selectByUserId( support.getUserId());
            user.setSupport(user.getSupport()-1);
            loginMapper.updateUser(user);
        }catch (Exception e){
            return ResponseResult.error("500");
        }
        return ResponseResult.success();
    }

    @Override
    public ResponseResult comment(Comment comment) {
        User user=loginMapper.selectByUserId(ThreadLocalUtil.get());
        if(ThreadLocalUtil.get()==null){
            System.out.println("用户不存在");
        }
        comment.setUserName(user.getUsername());
        comment.setCommentId(IdProcess.generateCommentId());
        comment.setCommentTime(LocalDateTime.now());
        comment.setCommentSupport(0);
        comment.setUserId(user.getId());

        Message message=new Message(comment);
        message.setSenderId(ThreadLocalUtil.get());
        Long id=null;
        if(comment.getParentId()!=null){
            Comment comment1=postMapper.selectComment(comment.getParentId());
            id=comment1.getUserId();
        }else{
            Post post=postMapper.getPost(comment.getPostId());
            id=post.getPostMaster();
        }
        message.setUserlId(id);
        messageMapper.addMessage(message);
        comment.setMessageId(message.getId());
        postMapper.newComment(comment);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult backPost(Post post) {
        post.setPostId(IdProcess.generatePostId());
        post.setTime(LocalDateTime.now());
        post.setTitle("");
        postMapper.backPost(post);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult backPostList(String postId) {
        log.info("回帖"+postId);
        List<Post>postList=postMapper.getBackPostList(postId);
        for(Post post:postList){
            post.setPostAvater(loginMapper.selectByUserId(post.getPostMaster()).getAvator());
            post.setUser(loginMapper.selectByUserId(post.getPostMaster()));
        }
        if(ThreadLocalUtil.get()!=null){
            for(Post post:postList){
                Support supporter=new Support();
                supporter.setUserId(ThreadLocalUtil.get());
                supporter.setPostId(post.getPostId());
                Support support=postMapper.getSupport(supporter);
                post.setUser(loginMapper.selectByUserId(post.getPostMaster()));

                log.info("post:{}",post.getCommentList());
                if(support==null){
                    post.setBeSupport(false);
                }else{
                    post.setBeSupport(true);
                }
                post.getCommentList().forEach(commentVo -> {
                    System.out.println("评论"+commentVo);
                    supporter.setPostId(commentVo.getCommentId());
                    Support commentSupport=postMapper.getSupport(supporter);
                    if(commentSupport==null){
                        commentVo.setBeSupport(false);
                    }else{
                        commentVo.setBeSupport(true);
                    }
                });
            }
        }else{

        }
        log.info("寻找回帖列表");
        return ResponseResult.success(postList);
    }

    @Override
    public List<Post> getPostList(String sectionId) {
        List<Post> postList=postMapper.getPostList(sectionId);
        for(Post post:postList){
            post.setPostAvater(loginMapper.selectByUserId(post.getPostMaster()).getAvator());
        }
        return postList;
    }


    @Override
    public void addPost(Post post) {
        post.setPostId(IdProcess.generatePostId());
        post.setTime(LocalDateTime.now());
        Message message=new Message(post);
        post.setMessage_id(message.getId());
        postMapper.addPost(post);
        messageMapper.addMessage(message);

    }

    @Override
    public List<Post> getOrderPostList(String sectionId) {
        List<Post> postList=postMapper.getOrderPostList(sectionId);
        for(Post post:postList){
            post.setPostAvater(loginMapper.selectByUserId(post.getPostMaster()).getAvator());
        }
        return postList;
    }

    @Override
    public ResponseResult getUserPostList(UserDto userDto) {
        List<Post>userPostList=postMapper.getUserPostList(userDto.getId());
        System.out.println("你们好");
        return ResponseResult.success(userPostList);
    }
}
